package com.rd.student.jike.class2.lihongming.Slolution3;

import sun.misc.Lock;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Auther:Heinrich
 * @Date:2023/7/19-07-19-16:07
 * @Description:com.rd.student.jike.class2.lihongming.Solution719
 * @version:1.0
 */
public class Question3 {
    public static void main(String[] args) {
        Variate variate = new Variate();
        ThreadNumAdd1 threadNumAdd1 = new ThreadNumAdd1(variate);
        ThreadNumSub1 threadNumSub1 = new ThreadNumSub1(variate);
        new ThreadNumAdd("加法1",variate).start();
        new Thread(threadNumAdd1,"加法2").start();
        new ThreadNumSub("减法1",variate).start();
        new Thread(threadNumSub1,"减法2").start();

    }
}
class ThreadNumAdd extends Thread{
    Variate variate;

    public ThreadNumAdd(String name, Variate variate) {
        super(name);
        this.variate = variate;

    }

    @Override
    public void run() {
        synchronized (variate){
            variate.setVar(variate.getVar()+1);
            System.out.println(getName()+" : "+variate.getVar());
        }
    }
}
//class ThreadNumAdd1 extends Thread{
//    AtomicInteger atVar ;
//
//    public ThreadNumAdd1(String name, int atVar) {
//        super(name);
//        this.atVar = new AtomicInteger(atVar);
//    }
//
//    @Override
//    public void run() {
//
//        System.out.println(getName()+" : "+atVar);
//    }
//}
class ThreadNumAdd1 implements Runnable {
    Variate variate;

    public ThreadNumAdd1(Variate variate) {
        this.variate = variate;
    }

    @Override
    public void run() {
        variate.setVar(variate.getVar() + 1);
        System.out.println(Thread.currentThread().getName() + " : " + variate.getVar());
    }
}
class ThreadNumSub1 implements Runnable {
    Variate variate;

    public ThreadNumSub1(Variate variate) {
        this.variate = variate;
    }

    @Override
    public void run() {
        variate.setVar(variate.getVar() - 1);
        System.out.println(Thread.currentThread().getName() + " : " + variate.getVar());
    }
}
class ThreadNumSub extends Thread{
    Variate variate;

    public ThreadNumSub(String name, Variate variate) {
        super(name);
        this.variate = variate;

    }

    @Override
    public void run() {
        synchronized (variate){
            variate.setVar(variate.getVar()-1);
            System.out.println(getName()+" : "+variate.getVar());
        }
    }
}

class Variate{
    private int var = 0;

    public int getVar() {
        return var;
    }

    public void setVar(int var) {
        this.var = var;
    }
    static ReentrantLock lock = new ReentrantLock();
    public static void add(Variate variate){
       lock.lock();
        variate.setVar(variate.getVar() + 1);
        System.out.println(Thread.currentThread().getName() + " : " + variate.getVar());
        lock.unlock();
    }
    public static void sub(Variate variate){
        lock.lock();
        variate.setVar(variate.getVar() - 1);
        System.out.println(Thread.currentThread().getName() + " : " + variate.getVar());
        lock.unlock();
    }
}